package org.zjh.controller.listener;

import org.zjh.controller.GameController;
import org.zjh.model.RealPlayer;
import org.zjh.utils.PokerUtil;
import org.zjh.view.game.GamePanel;
import org.zjh.view.ui.GameUi;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @description: 游戏面板监听
 * @author 郑佳豪
 * @date 2024/10/14 15:44
 * @version 1.0
 */
public class GamePanelLis extends MouseAdapter {

    private static final GamePanelLis gamePanelLis = new GamePanelLis();


    private GamePanelLis() {
    }


    @Override
    public void mousePressed(MouseEvent e) {
        checkClicked(e.getPoint());
    }


    private void checkClicked(Point point) {

        //获取控制器
        GameController gameController = GameController.getInstance();
        //获取游戏面板以及叫分标签
        GamePanel gamePanel = GameUi.gameFrame.getGamePanel();
        //1分标签
        JLabel onePointJLabel = gamePanel.getOnePointJLabel();
        //2分标签
        JLabel twoPointJLabel = gamePanel.getTwoPointJLabel();
        //3分标签
        JLabel threePointJLabel = gamePanel.getThreePointJLabel();
        //不叫标签
        JLabel noCallJLabel = gamePanel.getNoCallJLabel();
        //提示标签
        JLabel promptLabel = gamePanel.getPromptJLabel();
        //出牌标签
        JLabel discardPokersLabel = gamePanel.getDiscardPokersJLabel();
        //不要标签
        JLabel noDiscardPokersLabel = gamePanel.getNoDiscardPokersJLabel();
        //机器人标签
        JLabel machineHostJLabel = gamePanel.getMachineHostJLabel();
        //创建对应矩形对象
        //1分 矩形对象
        Rectangle onePointRectangle = createRectangle(onePointJLabel);
        //2分 矩形对象
        Rectangle twoPointRectangle = createRectangle(twoPointJLabel);
        //3分 矩形对象
        Rectangle threePointRectangle = createRectangle(threePointJLabel);
        //不叫 矩形对象
        Rectangle noCallPointRectangle = createRectangle(noCallJLabel);
        //提示 矩形对象
        Rectangle promptRect = createRectangle(promptLabel);
        //出牌 矩形对象
        Rectangle discardRect = createRectangle(discardPokersLabel);
        //不要 矩形对象
        Rectangle noDiscardRect = createRectangle(noDiscardPokersLabel);
        //机器人矩形对象
        Rectangle machineRect = createRectangle(machineHostJLabel);

        //获取实际玩家的 叫分结果标签
        JLabel realPlayerCallPointResultJLabel = gamePanel.getRealPlayerCallPointResultJLabel();
        //叫分结果标签值
        int callPoint = 0;
        String callPointResult = "";
        if (onePointRectangle.contains(point) && onePointJLabel.isVisible()) {
            callPoint = 1;
            callPointResult = "1分";
            gamePanel.getMultipleJLabel().setText("倍数：1");
            realPlayerCallPointResultJLabel.setText(callPointResult);
            gamePanel.setCallPointJLabelVisibleFalse();
        } else if (twoPointRectangle.contains(point) && twoPointJLabel.isVisible()) {
            callPoint = 2;
            callPointResult = "2分";
            gamePanel.getMultipleJLabel().setText("倍数：2");
            realPlayerCallPointResultJLabel.setText(callPointResult);
            gamePanel.setCallPointJLabelVisibleFalse();
        } else if (threePointRectangle.contains(point) && threePointJLabel.isVisible()) {
            callPoint = 3;
            callPointResult = "3分";
            gamePanel.getMultipleJLabel().setText("倍数：3");
            realPlayerCallPointResultJLabel.setText(callPointResult);
            gamePanel.setCallPointJLabelVisibleFalse();
        } else if (noCallPointRectangle.contains(point) && noCallJLabel.isVisible()) {
            callPointResult = "不叫";
            realPlayerCallPointResultJLabel.setText(callPointResult);
            gamePanel.setCallPointJLabelVisibleFalse();
        } else if (promptRect.contains(point) && promptLabel.isVisible()) {
            //执行出牌提示
            //获取能出的牌 并弹出
            PokerUtil.promptDiscardPoker(gameController.getPlayer(),gameController.getNowDeskPokerList());

        } else if (discardRect.contains(point) && discardPokersLabel.isVisible()) {
            //执行 出牌
            //首先获取 要出的牌的集合 判断牌型
            gameController.getPlayer().discardPoker();
        } else if (noDiscardRect.contains(point) && noDiscardPokersLabel.isVisible()) {
            //执行不出
            gamePanel.getRealNoDiscardJLabel().setText("不要");
            gameController.getPlayer().setDiscardPokerOver(true);
            //将点击来的手牌放下
            if (!gameController.getPlayer().getDiscardPokerList().isEmpty()){
                gameController.getPlayer().downDiscardPoker(gameController.getPlayer().getDiscardPokerList());
            }
        } else if (machineRect.contains(point)) {
            //点击启动（true） 再次点击 关闭(false)
            gameController.getPlayer().setHosted(!gameController.getPlayer().isHosted());
        }
        gamePanel.repaint();
        //给实际玩家叫分结果赋值
        GameController.getInstance().getPlayer().setCallPointResult(callPoint);

    }


    /**
     * 创建矩形对象方法
     * @param jLabel 标签对象
     * @return 矩形对象
     */
    private Rectangle createRectangle(JLabel jLabel) {
        return new Rectangle(jLabel.getX(), jLabel.getY(), jLabel.getWidth(), jLabel.getHeight());
    }


    public static GamePanelLis getInstance() {
        return gamePanelLis;
    }

}
